using System.Collections.Generic;
using Headsight.BPMN2WF.BPMN.Model;

namespace Headsight.BPMN2WF.Transformation.Transformers.Additions
{
    /// <summary>
    /// <see cref="IBPMN2WFTransformation"/> implementation looking
    /// up and returning the desired transformation result from the passed in <see cref="IDictionary{TKey,TValue}"/>
    /// and if not existing retrieving and caching the desired transformation result from the passed
    /// in <see cref="IBPMN2WFTransformation"/> delegate.
    /// </summary>
    public class BPMN2WFTransformationCache : BPMN2WFTransformationDelegateBaseImpl
    {
        public BPMN2WFTransformationCache(IDictionary<BPMNFlowNode, BPMNTransformationResult> cache, IBPMN2WFTransformation @delegate) : base(@delegate)
        {
            Cache = cache;
        }

        public IDictionary<BPMNFlowNode, BPMNTransformationResult> Cache { get; private set; }

        public override BPMNTransformationResult Transform(IBPMN2WFTransformationContext ctx, BPMNFlowNode flowNode)
        {
            if (Cache.ContainsKey(flowNode))
            {
                return Cache[flowNode];
            }

            var result = Delegate.Transform(ctx, flowNode);

            Cache[flowNode] = result;

            return result;
        }


    }
}